package com.bootdo.system.dao;

import com.bootdo.system.domain.Audit;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AuditMapper {

	@Select("select * from tb_audit where id = #{id}")
	Audit get(Long id);
	
	@Select("<script>" +
	"select * from tb_audit " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"bzId != null and bzId != ''\">"+ "and bzId = #{bzId} " + "</if>" + 
	  		 "<if test=\"bzType != null and bzType != ''\">"+ "and bzType = #{bzType} " + "</if>" + 
	  		 "<if test=\"auditResult != null and auditResult != ''\">"+ "and auditResult = #{auditResult} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<Audit> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select count(*) from tb_audit " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"bzId != null and bzId != ''\">"+ "and bzId = #{bzId} " + "</if>" + 
	  		 "<if test=\"bzType != null and bzType != ''\">"+ "and bzType = #{bzType} " + "</if>" + 
	  		 "<if test=\"auditResult != null and auditResult != ''\">"+ "and auditResult = #{auditResult} " + "</if>" + 
		  	"</where>"+ 
			"</script>")
	int count(Map<String,Object> map);
	
	@Insert("insert into tb_audit (`bzId`, `auditMemo`, `auditResult`, `bzType`, `createTime`)"
	+ "values (#{bzId}, #{auditMemo}, #{auditResult}, #{bzType}, now())")
	@Options(useGeneratedKeys=true)
	int save(Audit bComments);
	
	@Update("<script>"+ 
			"update tb_audit " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"bzId != null\">`bzId` = #{bzId}, </if>" + 
                    "<if test=\"auditMemo != null\">`auditMemo` = #{auditMemo}, </if>" + 
                    "<if test=\"bzType != null\">`bzType` = #{bzType}, </if>" + 
                    "<if test=\"auditResult != null\">`auditResult` = #{auditResult}, </if>" + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(Audit bComments);
	
	@Delete("delete from tb_audit where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_audit where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
}
